﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Facebook;

namespace SVLvsXNA.Facebook
{
    public partial class LoginPage : PhoneApplicationPage
    {
       

        private FacebookClient _fbClient = new FacebookClient();

        public FacebookOAuthResult _FacebookOAuthResult;
        public FacebookOAuthResult FacebookOAuthResult { get { return _FacebookOAuthResult; } }
        public string AccessToken;


        public LoginPage()
        {
            InitializeComponent();
            
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            
        }

        private void myWeb_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            FacebookOAuthResult oauthResult;
            if (!_fbClient.TryParseOAuthCallbackUrl(e.Uri, out oauthResult))
            {
                return;
            }

            if (oauthResult.IsSuccess)
            {
                AccessToken = oauthResult.AccessToken;
                FacebookServices.GetInstanse().AccessToken = AccessToken;
                //MessageBox.Show("Login success!\n" + AccessToken);
                NavigationService.Navigate(new Uri("/GamePage.xaml", UriKind.Relative));
            }
            else
            {
                MessageBox.Show(oauthResult.ErrorDescription);
            }
        }

        private void myWeb_Loaded(object sender, RoutedEventArgs e)
        {
            var parameters = new Dictionary<string, object>();
            var actions = new Dictionary<string, object>();
            actions["name"] = "get reward";
            actions["link"] = "http://example.com/redeem_reward";

            parameters["client_id"] = FacebookServices.AppID;
            parameters["redirect_uri"] = "https://www.facebook.com/connect/login_success.html";
            parameters["response_type"] = "token";
            parameters["display"] = "popup";
            parameters["scope"] = FacebookServices.ExtendedPermissions;
            parameters["actions"] = actions;

            Uri loginUri = _fbClient.GetLoginUrl(parameters);

            this.myWeb.Navigate(loginUri);
        }
    }
}